package edu.mse.camel.client.posterboard.ui.treeoutline;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;

import com.cea.papyrus.core.treeeditpart.TreeEditPartFactory;

import edu.mse.camel.client.posterboard.ui.editor.PosterBoardModelManager;
import edu.mse.camel.client.whiteboard.gef.ui.editpart.WhiteboardTreeEditpartfactory;

public class PosterboardTreeEditpartFactory implements EditPartFactory {

	private List<EditPartFactory> factories;
	private PosterBoardModelManager pbModelManager;
	
	public PosterboardTreeEditpartFactory(PosterBoardModelManager pbModelManager) {
		this.factories = new ArrayList<EditPartFactory>();
		this.pbModelManager = pbModelManager;
		initializeFactories();
	}
	
	private void initializeFactories() {
		TreeEditPartFactory papyrusFactory = 
			new TreeEditPartFactory(pbModelManager.getPapyrusModelManager());
		WhiteboardTreeEditpartfactory wbFactory = 
			new WhiteboardTreeEditpartfactory();
		this.factories.add(papyrusFactory);
		this.factories.add(wbFactory);
	}

	public void addEditPartFactory(EditPartFactory factory) {
		this.factories.add(factory);
	}
	
	@Override
	public EditPart createEditPart(EditPart context, Object model) {
		if (model instanceof PosterBoardModelManager) {
			return new PosterboardTreeEditPart(pbModelManager);
		}
		for (EditPartFactory aFactory : factories) {
			EditPart ep = aFactory.createEditPart(context, model);
			if (ep != null) {
				return ep;
			}
		}
		return null;
	}

}
